package javaSenior.threadExer;

/**
 * @author Lenl
 * @version v1.0
 * @create 2021-06-02 23:01
 * @description
 *
 * 关于同步方法的总结：
 *  1.同步方法仍然涉及到同步监视器，只是不需要我们显式的声明
 *  2.非静态的同步方法，同步监视器是this，静态的是当前类本身
 *
 *
 */
public class WindowTest3 {
    public static void main(String[] args) {
        window2 w2=new window2();
        Thread t1=new Thread(w2);
        Thread t2=new Thread(w2);
        Thread t3=new Thread(w2);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}


class window2 implements Runnable {
    private int ticket = 10000;
    @Override
    public void run() {
        while (true) {
            if(!show())break;
        }
    }

    private synchronized boolean show(){
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "买票，票号为：" + ticket--);
            return true;
        }else{
            return  false;
        }
    }

}